package threadcoreknowledge.threadobjectclasscommonmethods;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SleepInterrupted
 *  TODO 每隔1秒钟输出当前时间，被中断，观察。
 *               TODO Thread.sleep()
 *               TODO TimeUnit.SECONDS.sleep()
 * @Author wushaopei
 * @Date 2021/4/21 13:44
 * @Version 1.0
 */
public class SleepInterrupted implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++){
            System.out.println(new Date());
            try {
                TimeUnit.SECONDS.sleep(1);
                TimeUnit.HOURS.sleep(3);
                TimeUnit.MINUTES.sleep(25);
            } catch (InterruptedException e) {
                // 如果抛出异常，说明在睡眠时间内被中断了
                System.out.println("我被中断了");
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
         Thread thread = new Thread(new SleepInterrupted());
         thread.start();
         Thread.sleep(6500);
         thread.interrupt();
    }
}
